<template>
  <div class="manager_plant">
    <section class="manager_plant_header">
      <a-card>
        <a-form name="advanced_search" autocomplete="off" ref="searchFormRef" class="ant-advanced-search-form"
          :model="searchFormState">
          <a-row :gutter="24">
            <a-col :span="12">
              <a-form-item name="nickname" label="植物名称">
                <a-input v-model:value="searchFormState.nickname" placeholder="请输入植物名称" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item name="variety" label="品种">
                <a-input v-model:value="searchFormState.variety" placeholder="请输入品种" allowClear />
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24" style="text-align: right">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button style="margin: 0 8px" @click="handleReset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
    </section>
    <section class="manager_plant_content">
      <a-card style="height: 100%">
        <a-row style="margin-top: 8px;">
          <a-col :span="24">
            <a-table :dataSource="dataSource" :columns="columns" size="small" :pagination="false" :scroll="{ x: 1000 }">

              <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'cover'">
                  <a-image :src="`/api${record.cover}`" :width="100" :height="60" v-if="record?.cover" />
                </template>
              </template>
            </a-table>
          </a-col>
        </a-row>
      </a-card>
    </section>
    <footer class="manager_plant_footer">
      <a-card style="height: 100%;">
        <a-row align="middle">
          <a-col :span="24">
            <a-pagination :total="total" v-model:current="current" v-model:pageSize="pageSize" :showSizeChanger="true"
              showQuickJumper :show-total="total => `共 ${total} 条数据`" @change="onPageChange" />
          </a-col>
        </a-row>
      </a-card>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getPlantList} from '@/apis/plant';
const searchFormRef = ref();
const dataSource = ref([]);
const searchFormState = ref({
  nickname: "",
  variety:"",
});

const current = ref(1);
const total = ref(0);
const pageSize = ref(10);
const handleSearch = async () => {
  try {
    const { data={} } = await getPlantList({
      pageNum: current.value,
      pageSize: pageSize.value,
      nickname: searchFormState.value.nickname,
      variety: searchFormState.value.variety,
    });
    dataSource.value = data?.list;
    total.value = data?.total;
  } catch (error) {
    console.log(error, 'get-plant-list-error');
  }

};
const onPageChange = (page, Size) => {
  current.value = page;
  pageSize.value = Size;
  init();
};

const handleReset = () => {
  searchFormRef.value?.resetFields();
  init();
};
const init = async () => {
  try {
    const {data={}} = await getPlantList({
      pageNum: current.value,
      pageSize: pageSize.value,
    });
    dataSource.value = data?.list;
    total.value = data?.total;
  } catch (error) {
    console.log(error, 'get-plant-list-error');
  }
};

onMounted(() => {
  init();
});


const columns = [
  {
    title: '植物图片',
    dataIndex: 'cover',
    width: 160,
    align: 'center',
  },
  {
    title: '植物名称',
    dataIndex: 'nickname',
    width: 160,
    align: 'center',
  },
  {
    title: '品种',
    dataIndex: 'variety',
    width: 160,
    align: 'center',
  },
  {
    title: '种植日期',
    dataIndex: 'plantDate',
    width: 160,
    align: 'center',
  },
  {
    title: '地理位置',
    dataIndex: 'address',
    width: 230,
    align: 'center',
    ellipsis:true
  },
];
</script>

<style lang="less" scoped>
.manager_plant {
  box-sizing: border-box;
  padding: 8px;
  display: flex;
  flex-direction: column;
  height: 100%;

  &_header {
    width: 100%;
    max-height: 200px;
  }

  &_content {
    margin-top: 8px;
    flex: 1;
    overflow: auto;
  }

  &_footer {
    margin-top: 8px;
    height: 80px;
  }
}
</style>
